import datetime

from django.contrib import admin

# Register your models here.
from django.db.models import Sum, Q

from common.models import Person


# class InfosInline(admin.TabularInline):
#     model = Information
#     extra = 1


class PersonAdmin(admin.ModelAdmin):
    list_display = ["name", "time_last_mon_0", "time_last_mon_1", "time_last_mon_2", "leave_last_days",
                    "leave_last_times", "time_this_mon_0", "time_this_mon_1", "time_this_mon_2",
                    "leave_this_days", "leave_this_times", "work_or_leave_person"]

    list_filter = ["work_or_leave", ]

    ordering = ["-work_or_leave", "-leave_time", "entry_time"]

    # inlines = [
    #     InfosInline,
    # ]

    def time_last_base(self, db, num):
        q = db.per_work.filter(work_time__contains=(datetime.datetime.now().replace(day=1) -
                                                    datetime.timedelta(days=1)).replace(day=1).strftime("%Y-%m"))
        q = q.filter(week_today=num)
        q = 0 if q.aggregate(all=Sum("time_long"))["all"] is None else q.aggregate(all=Sum("time_long"))["all"]
        return "%.2f" % q

    def time_this_base(self, db, num):
        q = db.per_work.all().filter(
            work_time__contains=(datetime.datetime.now().replace(day=1)).strftime("%Y-%m"))
        q = q.filter(week_today=num)
        q = 0 if q.aggregate(all=Sum("time_long"))["all"] is None else q.aggregate(all=Sum("time_long"))["all"]
        return "%.2f" % q

    def time_last_mon_0(self, db):
        return self.time_last_base(db, "0")
        # q = db.per_work.filter(work_time__contains=(datetime.datetime.now().replace(day=1) -
        #                                               datetime.timedelta(days=1)).replace(day=1).strftime("%Y-%m"))
        # q.filter(week_today="0")
        # return q.aggregate(all=Sum("time_long"))["all"]

    def time_last_mon_1(self, db):
        return self.time_last_base(db, "1")

    def time_last_mon_2(self, db):
        return self.time_last_base(db, "2")

    def time_this_mon_0(self, db):
        return self.time_this_base(db, "0")
        # q = db.per_work.all().filter(
        #     work_time__contains=(datetime.datetime.now().replace(day=1)).strftime("%Y-%m"))
        # q = q.filter(week_today="0")
        # return q.aggregate(all=Sum("time_long"))["all"]

    def time_this_mon_1(self, db):
        return self.time_this_base(db, "1")

    def time_this_mon_2(self, db):
        return self.time_this_base(db, "2")

    def leave_base(self, db, month, is_day):
        if month == "last":
            q = db.leave_record.filter(leave_time__contains=(
                    datetime.datetime.now().replace(day=1) - datetime.timedelta(days=1)).replace(day=1).strftime(
                "%Y-%m"))
        elif month == "this":
            q = db.leave_record.all().filter(
                leave_time__contains=(datetime.datetime.now().replace(day=1)).strftime("%Y-%m"))
        else:
            return "error"

        if is_day:
            q = 0 if q.aggregate(all=Sum("account"))["all"] is None else q.aggregate(all=Sum("account"))["all"]
        else:
            q = 0 if q.aggregate(all=Sum("account"))["all"] is None else q.aggregate(all=Sum("account"))["all"] * 8
        return "%.1f" % q

    def leave_last_days(self, db):
        return self.leave_base(db, "last", True)

    def leave_last_times(self, db):
        return self.leave_base(db, "last", False)

    def leave_this_days(self, db):
        return self.leave_base(db, "this", True)

    def leave_this_times(self, db):
        return self.leave_base(db, "this", False)

    time_last_mon_0.__name__ = "上月-工作日"
    time_last_mon_1.__name__ = "上月-周末"
    time_last_mon_2.__name__ = "上月-法定节假日"

    time_this_mon_0.__name__ = "本月-工作日"
    time_this_mon_1.__name__ = "本月-周末"
    time_this_mon_2.__name__ = "本月-法定节假日"

    leave_last_days.__name__ = "上月-请假天数"
    leave_last_times.__name__ = "上月-请假时长"

    leave_this_days.__name__ = "本月-请假天数"
    leave_this_times.__name__ = "本月-请假时长"

    def work_or_leave_person(self, db):
        return "在职" if db.work_or_leave else "{} 离职".format(db.leave_time.strftime("%m月%d日"))
    work_or_leave_person.__name__ = "是否在职"

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.filter(Q(work_or_leave=True) | Q(Q(work_or_leave=False) & Q(leave_time__gte=(
                datetime.datetime.now().replace(day=1)-datetime.timedelta(days=1)).replace(day=1).strftime("%Y-%m-%d"))))


admin.site.register(Person, PersonAdmin)
admin.site.site_header = '天下财经员工管理系统'
admin.site.site_title = '天下财经员工管理系统'